<?php
namespace app\common\utils;

class BaiduOcr {
    const API_KEY = "brMCaVwYf4bc44B8f0oQjg3D";
    const SECRET_KEY = "vCRNfbxIUXEUD1DQQxsJ1F5mbazLhqxG";

    public $key = "";
    public $secret = "";

    public function __construct($key, $secret){
        $this->key = $key;
        $this->secret = $secret;
    }

    public function run($file, $options) {
        $options = $options ?? [];
        if (empty($options['type'])) {
            $options['type'] = 'image';
        }
        $fields = [
            // 'image' => $image,
            'detect_direction' => 'false',
            'detect_language' => 'false',
            'paragraph' => 'false',
            'probability' => 'false'
        ];
        $fields[$options['type']] = $file;
        // var_dump($fields);
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token={$this->getAccessToken()}",
            CURLOPT_TIMEOUT => 30,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_SSL_VERIFYHOST  => false,
            CURLOPT_CUSTOMREQUEST => 'POST',
            
            CURLOPT_POSTFIELDS => http_build_query($fields),
    
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/x-www-form-urlencoded',
                'Accept: application/json'
            ),

        ));
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }

    public function run1($file, $options) {
        $options = $options ?? [];
        if (empty($options['type'])) {
            $options['type'] = 'image';
        }
        $fields = [
            // 'image' => $image,
            'detect_direction' => 'false',
            'paragraph' => 'false',
            'probability' => 'false',
            'multidirectional_recognize' => 'false'
        ];
        $fields[$options['type']] = $file;
        $fields['pdf_file_num'] = $options['pdf_file_num'];
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token={$this->getAccessToken()}",
            CURLOPT_TIMEOUT => 30,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_SSL_VERIFYHOST  => false,
            CURLOPT_CUSTOMREQUEST => 'POST',
            
            CURLOPT_POSTFIELDS => http_build_query($fields),
            
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/x-www-form-urlencoded',
                'Accept: application/json'
            ),

        ));
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }
    
    /**
        * 使用 AK，SK 生成鉴权签名（Access Token）
        * @return string 鉴权签名信息（Access Token）
        */
    private function getAccessToken(){
        $curl = curl_init();
        $postData = array(
            'grant_type' => 'client_credentials',
            'client_id' => $this->key ?? self::API_KEY,
            'client_secret' => $this->secret ?? self::SECRET_KEY
        );
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://aip.baidubce.com/oauth/2.0/token',
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_SSL_VERIFYHOST  => false,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => http_build_query($postData)
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        $rtn = json_decode($response);
        return $rtn->access_token;
    }
}